package com.haidong.service.impl;

import com.haidong.dao.BookDao;
import com.haidong.dao.BrandDao;
import com.haidong.dao.impl.BookDaoImpl;
import com.haidong.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
    // 删除业务层中的new对象,转而用IOC容器提供
    // 使用DI为容器中的不同bean提供依赖
    private BookDao bookDao;
    private BrandDao brandDao;

    @Override
    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
        brandDao.run();
    }
    // 对外提供setter
    public void setBookDao(BookDao bookDao){
        // 由于service的bean需要调用dao的bean,需要对外设置方法用来注入bean
        // 由于接口实现的init函数是属性配置后的,所以在bean注入之后才会执行
        System.out.println("dao.bean注入");
        this.bookDao = bookDao;
    }

    public BookServiceImpl(BrandDao brandDao) {
        this.brandDao = brandDao;
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("销毁接口执行");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化函数是在该类的bean属性加载之后才执行");
    }
}
